import { request } from '@/utils/request'
import type {
  DrawHistory,
  DrawResponse,
  DrawFilterDto,
  Restaurant,
  PaginatedDrawHistory,
  DrawStats,
  RestaurantDrawCount,
  PaginationParams,
  DateFilter,
  ApiResponse
} from './types'

/**
 * 抽签相关API
 */
export const drawApi = {
  /**
   * 开始抽签
   * @param filters 可选的筛选条件
   */
  startDraw(filters?: DrawFilterDto) {
    return request.post<ApiResponse<DrawResponse>>('/draw/start', filters)
  },

  /**
   * 重新抽签
   * @param filters 可选的筛选条件
   */
  redraw(filters?: DrawFilterDto) {
    return request.post<ApiResponse<DrawResponse>>('/draw/restart', filters)
  },

  /**
   * 获取符合条件的餐厅列表
   * @param filters 筛选条件
   */
  getEligibleRestaurants(filters?: DrawFilterDto) {
    return request.get<ApiResponse<Restaurant[]>>('/draw/eligible', filters)
  },

  /**
   * 获取抽签历史（分页）
   * @param pagination 分页参数
   * @param dateFilter 日期筛选
   */
  getDrawHistory(pagination?: PaginationParams, dateFilter?: DateFilter) {
    const params = { ...pagination, ...dateFilter }
    return request.get<ApiResponse<PaginatedDrawHistory>>('/draw/history', params)
  },

  /**
   * 获取最近的抽签历史
   * @param limit 限制数量，默认10条
   */
  getRecentDrawHistory(limit: number = 10) {
    return request.get<ApiResponse<DrawHistory[]>>('/draw/history/recent', { limit })
  },

  /**
   * 获取今天的抽签历史
   */
  getTodayDrawHistory() {
    return request.get<ApiResponse<DrawHistory[]>>('/draw/history/today')
  },

  /**
   * 获取本周的抽签历史
   */
  getWeekDrawHistory() {
    return request.get<ApiResponse<DrawHistory[]>>('/draw/history/week')
  },

  /**
   * 获取本月的抽签历史
   */
  getMonthDrawHistory() {
    return request.get<ApiResponse<DrawHistory[]>>('/draw/history/month')
  },

  /**
   * 删除抽签历史记录
   * @param id 历史记录ID
   */
  deleteDrawHistory(id: string) {
    return request.delete<ApiResponse<void>>(`/draw/history/${id}`)
  },

  /**
   * 清空抽签历史
   */
  clearDrawHistory() {
    return request.delete<ApiResponse<void>>('/draw/history/clear')
  },

  /**
   * 获取抽签统计信息
   */
  getDrawStats() {
    return request.get<ApiResponse<DrawStats>>('/draw/stats')
  },

  /**
   * 获取餐厅抽签次数统计
   * @param restaurantId 餐厅ID（可选）
   */
  getRestaurantDrawCount(restaurantId?: string) {
    const params = restaurantId ? { restaurant_id: restaurantId } : {}
    return request.get<ApiResponse<RestaurantDrawCount[]>>('/draw/restaurant-count', params)
  }
}

export default drawApi